<?php

namespace think;
use think\facade\Env;
/**
 * 插件基类
 * Class Addns
 * @package think\addons
 */
abstract class Addons extends Controller
{
    // 当前错误信息
    protected $error;

    /**
     * $info = [
     *  'name'          => 'Test',
     *  'title'         => '测试插件',
     *  'description'   => '用于thinkphp5的插件扩展演示',
     *  'status'        => 1,
     *  'author'        => 'kingarthur',
     *  'version'       => '0.1'
     * ]
     */
    public $info = [];
    public $addons_path = '';
    public $config_file = '';
    // ================= 2021-03-09
    private $pluginPath = '';
    private $name = '';
    private $configFilePath = '';
    //================= 2021 -03-09
    // 初始化
    protected function initialize()
    {
        // 获取当前插件目录
        $this->addons_path = Env::get('addons_path') . $this->getName() . DIRECTORY_SEPARATOR;

        //================= 2021 -03-09
        $this->name = $this->getName();
        $nameCStyle = Loader::parseName($this->name);
        $this->pluginPath     = ROOT_PATH . 'addons/' . $nameCStyle . '/';
        $this->configFilePath = $this->pluginPath . 'config.php';
        //================= 2021 -03-09

        // 重新定义模板的根目录
//        if ($this->view) {
//            $this->view->config('view_path', $this->addons_path );
//        }
    }

    //必须实现安装
    abstract public function install();

    //必须卸载插件方法
    abstract public function uninstall();

    /**
     * 获取插件信息
     * @return array
     */
    final public function getInfo()
    {
        $info_path = $this->addons_path . 'info.ini';
        if (is_file($info_path)) {
            $info = parse_ini_file($info_path);
            if (is_array($info)) {
                $this->info = array_merge($this->info, $info);
            }
        }
        return $this->info;
    }

    // 获取扩展项目的配置信息
    public function getConfig()
    {
        $file = env('addons_path') . $this->getAddonName() . '/config.php';
        if (!file_exists($file)) return false;
        return require_once $file;
    }


    /**
     * 获取当前模块名
     * @return string
     */
    final public function getName()
    {
        $data = array_reverse(explode('\\', get_class($this)));
        return $data[1];
    }

    // 获取扩展配置基本信息
    public function getManifest()
    {
        $file = env('addons_path') . $this->getAddonName() . '/manifest.php';
        if (!file_exists($file)) return false;
        return require_once $file;
    }

    // 获取继承者的类名称  不含Addon的小写字母名称
    protected function getAddonName()
    {
        $arr = explode('\\', get_class($this));
        return $arr[1];
    }

    // 创建安装成功标识文件
    protected function createInstallFile()
    {
        $file = env('addons_path') . $this->getAddonName() . '/install.lock';
        if (file_exists($file)) return true;
        return file_put_contents($file, 'dir', FILE_USE_INCLUDE_PATH);
    }

    protected function deleteInstallFile()
    {
        $file = env('addons_path') . $this->getAddonName() . '/install.lock';
        if (!file_exists($file)) return true;
        return unlink($file);
    }

    /**
     * 重写模板渲染
     * @param string $template
     * @param array $vars
     * @param array $config
     * @return mixed|string
     * @throws \Exception
     */
    protected function fetch($template = '', $vars = [], $config = [])
    {
        // 拼接插件视图文件的默认目录
        return $this->view->fetch($this->addons_path.$template.'.'.config('template.view_suffix'), $vars, $config);
    }


    /**
     * 创建扩展
     * @param string $addonName
     * @param int $type 0单模块 1多模块
     * @param array $config 配置
     */
    public function createAddon($addonName = '', $type = 0, $config = [])
    {
        $addonName = strtolower($addonName);
        $filePath = env('addons_path') . $addonName;
        file_put_contents($filePath . '/config.php');
    }

    /**
     * 检查配置信息是否完整
     * @return bool
     */
    final public function checkInfo()
    {
        $info_check_keys = ['name', 'title', 'description', 'status', 'author', 'version'];
        foreach ($info_check_keys as $value) {
            if (!array_key_exists($value, $this->getInfo())) {
                return false;
            }
        }
        return true;
    }

    //==================================  20201-03-09  ===============//
    /**
     * 获取插件的配置数组
     * @return array
     */
    final public function getDefaultConfig()
    {
        $config = [];
        if (file_exists($this->configFilePath)) {
            $tempArr = include $this->configFilePath;
            if (!empty($tempArr) && is_array($tempArr)) {
                foreach ($tempArr as $key => $value) {
                    if ($value['type'] == 'group') {
                        foreach ($value['options'] as $gkey => $gvalue) {
                            foreach ($gvalue['options'] as $ikey => $ivalue) {
                                $config[$ikey] = $ivalue['value'];
                            }
                        }
                    } else {
                        $config[$key] = $tempArr[$key]['value'];
                    }
                }
            }
        }


        return $config;
    }

    /**
     * 获取插件的配置数组
     * @return array
     */
    final public function getConfigs()
    {
        $name = $this->getName();
        if (PHP_SAPI != 'cli') {
            static $_config = [];
            if (isset($_config[$name])) {
                return $_config[$name];
            }
        }

        $config = Db::name('plugin')->where('name', $name)->value('config');

        if (!empty($config) && $config != "null") {
            $config = json_decode($config, true);
        } else {
            $config = $this->getDefaultConfig();
        }
        $_config[$name] = $config;
        return $config;
    }

    /**
     * 获取插件配置文件绝对路径
     * @return string
     */
    final public function getConfigFilePath()
    {
        return $this->configFilePath;
    }
    //================================== 2021-03-09 ===============//

}
    
